/*
 * Project: Made 2 Order Software utility package
 * Package: com.made2ordersoftware.util
 * File:    MultiComparator.java
 * Created: Dec 31, 2010
 */

/*
 *  Copyright (c) 2010 Made 2 Order Software, LLC
 *
 *  All rights reserved. No part of this software may be reproduced, stored in a retrieval system, or
 *  transmitted in any form or by any means, without the prior written permission of Made 2 Order Software.
 *
 *  You may not distribute the URL or redistribute the files to third parties. You may transfer the files
 *  onto a single storage device so long as you can prove ownership of the software. You may not reverse
 *  engineer, decompile, or disassemble the software or media files. You may not modify or translate the
 *  software or media, or distribute copies of the software or media without the written consent of Made
 *  2 Order Software
 */
package com.made2ordersoftware.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class is used to compare multiple variables of an object. To use this class, you must have a
 * comparator class
 * which implements Comparator for each variable you wish to use.
 *
 * public class VariableComparator implements Comparator<EntityObject> { public int compare(EntityObject o1,
 * EntityObject o2) { return o1.getVariable() - o2.getVariable(); } }
 *
 * To implement you need a referense to the Entity being compared and add the variable comparator using the
 * addComparator method.
 *
 * MultiComparator mc = new MultiComparator(); EntityObject obj = new EntityObject(); mc.addComparator(obj.new
 * VariableComparator());
 *
 * @param <T>
 *
 * @author David Canning <dcannin@gmail.com>
 * @version 1.0.0
 */
public class MultiComparator<T> implements Comparator<T>, Serializable {

    private static final Logger LOG = LoggerFactory.getLogger(MultiComparator.class);
    private static final long serialVersionUID = 1L;
    private final List<Comparator<T>> comparators = new ArrayList<>();

    /**
     *
     * @param c
     */
    public void addComparator(Comparator<T> c) {
        comparators.add(c);
    }

    /**
     *
     * @param c
     */
    public void removeComparator(Comparator<T> c) {
        comparators.remove(c);
    }

    @Override
    @SuppressWarnings("unchecked")
    public int compare(Object obj1, Object obj2) {
        int result = 0;
        for (Comparator comp : comparators) {
            result = comp.compare(obj1, obj2);
            if (result != 0) {
                break;
            }
        }
        return result;
    }
}
